﻿using MMS.Api.Helper;
using MMS.Model.FormatModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.WebPages;
namespace MMS.Api.Areas.Admin.Controllers
{
    public class SalesPromotionController : BaseController
    {
        #region 1.0 分页查询  //http://localhost:16824/api/SalesPromotion/GetWherePaged
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">page页码</param>
        /// <param name="id1">rows页容量</param>
        /// <returns></returns>
        public PageData<Model.SalesPromotion> GetWherePaged(string id, string id1)
        {
            int pageIndex = id.AsInt();
            int pageSize = id1.AsInt();
            //1.查询数据集合
            var pageData = OpeCur.BLLSession.SalesPromotion.WherePaged(pageIndex, pageSize, o => o.SIsDel == false, o => o.SId);
            pageData.rows = pageData.rows.Select(o => o.ToPOCO()).ToList();
            //2.转成json格式字符串

            return pageData;
        }
        #endregion

        #region 2.0 新增 //http://localhost:16824/api/SalesPromotion/PostAdd
        public void PostAdd(Model.SalesPromotion model)
        {
            OpeCur.BLLSession.SalesPromotion.Add(model);
            OpeCur.BLLSession.SaveChanges();
        }
        #endregion

        #region 3.0 查找修改 //http://localhost:16824/api/SalesPromotion/GetModify/1
        public Model.SalesPromotion GetModify(int id)
        {
            var model = OpeCur.BLLSession.SalesPromotion.Where(o => o.SId == id && o.SIsDel == false).FirstOrDefault().ToPOCO();
            return model;
        }
        #endregion

        #region 3.1 修改 //http://localhost:16824/api/SalesPromotion/PostModify
        public void PostModify(Model.SalesPromotion model)
        {
            OpeCur.BLLSession.SalesPromotion.Modify(model, "LName", "LUpGradeAmount", "LDiscount", "LIntegralRatio", "LRemark");
            OpeCur.BLLSession.SaveChanges();
        }
        #endregion

        #region 4.0 删除 // http://localhost:16824/api/SalesPromotion/GetRemoveById/
        public void GetRemoveById(int id)
        {
            OpeCur.BLLSession.SalesPromotion.ModifyBy(o => o.SId == id, new string[] { "SIsDel" }, new object[] { true });
            OpeCur.BLLSession.SaveChanges();
        }
        #endregion

        #region 5.0 查询  //http://localhost:16824/api/SalesPromotion/GetWhere
        public List<Model.SalesPromotion> GetWhere()
        {
            var list = OpeCur.BLLSession.SalesPromotion.Where(o => o.SIsDel == false).ToList().Select(o => o.ToPOCO()).ToList(); ;
            return list;
        }
        #endregion

        #region 6.0 分页查询  //http://localhost:16824/api/SalesPromotion/GetWherePagedTradeInfoAnalyze
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">SMinMoney</param>
        /// <param name="id1">SMaxMoney</param>
        /// <param name="id2">SBeginTime</param>
        /// <param name="id3">SEndTime</param>
        /// <param name="id4">SPlace</param>
        /// <param name="id5">pageIndex</param>
        /// <param name="id6">pageSize</param>
        /// <returns></returns>
        public PageData<Model.MemberInfo> GetWherePagedPromotionAnalyze(string id, string id1, string id2, string id3, string id4, string id5, string id6)
        {
            DateTime SBeginTime = Convert.ToDateTime(id2);

            DateTime SEndTime = Convert.ToDateTime(id3);

            int pageIndex = id5.AsInt();
            int pageSize = id6.AsInt();
            int SMinMoney = id.AsInt();
            int SMaxMoney = id1.AsInt();
            //在指定日期（促销活动期）内有消费记录或达到指定消费金额的会员编号
            List<int?> Mids = OpeCur.BLLSession.TradeInfo.Where(o => o.TOriginalPay > SMinMoney && o.TOriginalPay < SMaxMoney && o.TAddress == id4 && o.TTime > SBeginTime && o.TTime <SEndTime).ToList().Select(o => o.TMId).ToList();
            //1.查询数据集合
            var pageData = OpeCur.BLLSession.MemberInfo.WherePaged(pageIndex, pageSize, o => o.MIsDel == false && Mids.Contains(o.MId), o => o.MId, true, "Level");
            pageData.rows = pageData.rows.Select(o => o.ToPOCO(true)).ToList();
            //2.转成json格式字符串

            return pageData;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">SMinMoney</param>
        /// <param name="id1">SMaxMoney</param>
        /// <param name="id2">SBeginTime</param>
        /// <param name="id3">SEndTime</param>
        /// <param name="id4">SPlace</param>
        /// <param name="id5">pageIndex</param>
        /// <param name="id6">pageSize</param>
        /// <returns></returns>
        public PageData<Model.TradeInfo> GetWherePagedTradeInfoAnalyze(string id, string id1, string id2, string id3, string id4, string id5, string id6)
        {
            DateTime SBeginTime = Convert.ToDateTime(id2);

            DateTime SEndTime = Convert.ToDateTime(id3);

            int pageIndex = id5.AsInt();
            int pageSize = id6.AsInt();
            int SMinMoney = id.AsInt();
            int SMaxMoney = id1.AsInt();
            //在指定日期（促销活动期）内有消费记录或达到指定消费金额的会员编号
            var pageData = OpeCur.BLLSession.TradeInfo.WherePaged(pageIndex, pageSize, o => o.TOriginalPay > SMinMoney && o.TOriginalPay < SMaxMoney && o.TAddress == id4 && o.TTime > SBeginTime && o.TTime < SEndTime, o => o.TMId, true, "MemberInfo");
            //1.查询数据集合
           
            pageData.rows = pageData.rows.Select(o => o.ToPOCO(true)).ToList();
            //2.转成json格式字符串

            return pageData;
        }
        #endregion

    }
}
